package com.zimmur.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.mysql.fabric.xmlrpc.base.Array;
import com.zimmur.common.AppModuleEnum;
import com.zimmur.common.IDWorkFactory;
import com.zimmur.common.ReturnJsonEntity;
import com.zimmur.common.StatusCodeEnum;
import com.zimmur.model.account.GoodsUser;
import com.zimmur.model.zone.Topic;
import com.zimmur.model.zone.TopicComment;
import com.zimmur.service.IUserService;
import com.zimmur.service.zone.ITopicCommentPraiseService;
import com.zimmur.service.zone.ITopicCommentService;
import com.zimmur.service.zone.ITopicPictureService;
import com.zimmur.service.zone.ITopicService;
import com.zimmur.tools.DateUtils;

@RestController  
@RequestMapping("/restful/v120/topic")  
public class TopicV120Controller extends BaseController{
	private static Logger log = LoggerFactory.getLogger(TopicController.class);
	@Resource
	private ITopicService topicService;
	@Resource
	private ITopicPictureService topicPictureService;
	@Resource  
	private ITopicCommentService topicCommentService;
	@Resource
	private ITopicCommentPraiseService topicCommentPraiseService;
	@Value("#{spingMvcProperties['System.GuestId']}")
	private String guessUserId;
	@Resource  
	private IUserService userService;
	 
	 
	/**
	 * 话题详情，查看单个话题,评论
	 * @param request
	 * @param model
	 * @return
	 */
    @RequestMapping(value="/detail",method=RequestMethod.GET)  
    public ReturnJsonEntity topicDetail(){
    	ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		try{
			long topicId = getLongParam("topicId");
	        long userId= getTokenUserId();
	       
	        com.zimmur.restful.model.Topic topic = topicService.selectByTopicIdV120(topicId,userId);
	        
	        if(topic != null){
	        	entity.setData(topic);
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	        }else{
	        	entity.setData(new ArrayList<>());
	        	statusCodeEnum = StatusCodeEnum.CODE300404;
	        }
		} catch (Exception e) {
			log.error(e.getMessage());
			entity.setData(new ArrayList<>());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }	
    /**
	 * 评论点赞
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/commentpraise",method=RequestMethod.POST)  
    public ReturnJsonEntity commentpraise(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		//游客身份验证
		if(String.valueOf(getTokenUserId()).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
		try{
			//获取当前时间，转为INT存储
	        long commentId = getLongParam("commentId");
	        long userId= getTokenUserId();	       
		    Integer resut = topicCommentPraiseService.add(commentId, userId);
		    
	        if(resut > 0){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 评论列表，分页
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/commentlist",method=RequestMethod.GET)  
    public ReturnJsonEntity commentList(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
	        Integer pageNow = getIntParam("pageNow");
	        long topicId = getLongParam("topicId");
	        long userId= getTokenUserId();
	 
		    List<TopicComment> topicCommentList = topicCommentService.pageCommentList(topicId,userId, pageNow);
		    int count =topicCommentService.countByTopicIdTopicComment(topicId);
		    int maxPage=(count+10-1)/10;
		    Map<String, Object> map=new HashMap<>();
		    map.put("totalRecord", count);
		    map.put("commentList", topicCommentList);
		    map.put("pageNo", pageNow);
		    map.put("totalPage", maxPage);
		    map.put("pageSize", 10);
		    entity.setData(map);
		    statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 评论录入
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/addcomment",method=RequestMethod.POST)  
    public ReturnJsonEntity commentInsert(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		try{
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			
	        long commentId =IDWorkFactory.getID(AppModuleEnum.Zone);
	        long topicId = getLongParam("topicId");
			String content = getParam("content");
			String commentType=getParam("type");
			long userCommentId = getLongParam("userCommentId");
			//获取当前时间，转为INT存储
	        Integer createDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
	        GoodsUser createUser = userService.selectByUserId(getTokenUserId());
	        TopicComment topicComment=new TopicComment();
	        topicComment.setCommentId(commentId);
	        topicComment.setTopicId(topicId);
	        topicComment.setContent(content);
	        topicComment.setUserCommentId(userCommentId);
	        topicComment.setCreateUserId(createUser.getUserId());
	        topicComment.setCreateUserName(createUser.getNickName());
	        topicComment.setCreateCategoryName(createUser.getCategoryName());
	        topicComment.setCreateCode(createUser.getCategoryCode());
	        topicComment.setCreateDate(createDate);

	        Integer resut = topicCommentService.insertTopicComment(topicComment);
	        
	        if(resut > 0){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	        	//entity.setData(resut);
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
		
}
